package com.github.dongglin.smart.spring.boot.plus.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.binding.QueryBuilder;
import com.github.dongglin.smart.spring.boot.plus.common.exception.BusinessException;
import com.github.dongglin.smart.spring.boot.plus.core.framework.pager.PageResult;
import com.github.dongglin.smart.spring.boot.plus.core.framework.service.impl.OptionalBaseServiceImpl;
import com.github.dongglin.smart.spring.boot.plus.web.dto.FormInfoAddDTO;
import com.github.dongglin.smart.spring.boot.plus.web.dto.FormInfoUpdateDTO;
import com.github.dongglin.smart.spring.boot.plus.web.entity.FormInfo;
import com.github.dongglin.smart.spring.boot.plus.web.mapper.FormInfoMapper;
import com.github.dongglin.smart.spring.boot.plus.web.req.FormInfoRequest;
import com.github.dongglin.smart.spring.boot.plus.web.resp.FormInfoDetailVo;
import com.github.dongglin.smart.spring.boot.plus.web.resp.FormInfoResponse;
import com.github.dongglin.smart.spring.boot.plus.web.service.FormInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 *  服务实现类
 *
 * @author DongGL
 * @since 2023-09-20
 */
@Slf4j
@Service
@SuppressWarnings({"DuplicatedCode", "unused"})
public class FormInfoServiceImpl extends OptionalBaseServiceImpl<FormInfoMapper, FormInfo> implements FormInfoService {

    @Resource
    private FormInfoMapper formInfoMapper;

    /**
     * 添加
     *
     * @param formInfoAddDTO formInfoAddDTO
     * @return boolean
     * @throws Exception ex
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addFormInfo(FormInfoAddDTO formInfoAddDTO) throws Exception {
        FormInfo formInfo = new FormInfo();
        BeanUtils.copyProperties(formInfoAddDTO, formInfo);
        return save(formInfo);
    }

    /**
     * 修改
     *
     * @param formInfoUpdateDTO formInfoUpdateDTO
     * @return boolean
     * @throws Exception ex
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateFormInfo(FormInfoUpdateDTO formInfoUpdateDTO) throws Exception {
        Long id = formInfoUpdateDTO.getId();
        FormInfo formInfo = getById(id);
        if (formInfo == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(formInfoUpdateDTO, formInfo);
        return updateById(formInfo);
    }

    /**
     * 删除
     *
     * @param id id
     * @return boolean
     * @throws Exception ex
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteFormInfo(Long id) throws Exception {
        return removeById(id);
    }

    /**
     * 详情
     *
     * @param id id
     * @return FormInfoDetailVo
     * @throws Exception ex
     */
    @Override
    public FormInfoDetailVo getFormInfoById(Long id) throws Exception {
        return getByIdViewObjectOptionalOrElseThrow(id, FormInfoDetailVo.class);
    }

    /**
     * 分页列表
     *
     * @param formInfoRequest formInfoRequest
     * @return PageResult<FormInfoResponse>
     * @throws Exception ex
     */
    @Override
    public PageResult<FormInfoResponse> getFormInfoPagerList(FormInfoRequest formInfoRequest) throws Exception {
        QueryWrapper< FormInfo> queryWrapper = QueryBuilder.toQueryWrapper(formInfoRequest);
        return pagerListViewOptionalOrElseEmpty(queryWrapper, FormInfoResponse.class, formInfoRequest);
    }

}
